import {
  getGameListService,
  getApprovedGameListService,
  searchGameService
} from '@/api/game'

import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useGameStore = defineStore(
  'gameList',
  () => {
    // 游戏列表数据
    const game = ref([])
    
    // 获取已通过审核的游戏列表
    const getApprovedGameList = async () => {
      try {
        console.log('获取已审核游戏列表')
        const res = await getApprovedGameListService()
        game.value = res.data
        console.log('获取游戏列表成功，数量:', game.value.length)
      } catch (error) {
        console.error('获取游戏列表失败:', error)
      }
    }

    // 获取所有游戏列表
    const getGamelist = async () => {
      try {
        console.log('获取所有游戏列表')
        const res = await getGameListService()
        game.value = res.data
        console.log('获取所有游戏列表成功，数量:', game.value.length)
      } catch (error) {
        console.error('获取所有游戏列表失败:', error)
      }
    }
    
    // 搜索游戏
    const searchGame = async (data) => {
      try {
        console.log('搜索游戏:', data)
        const res = await searchGameService(data)
        if (res && res.data) {
          game.value = res.data
          console.log('搜索游戏成功，结果数量:', game.value.length)
        } else {
          console.error('搜索结果未包含数据:', res)
        }
      } catch (error) {
        console.error('搜索游戏失败:', error)
      }
    }
    
    return {
      game,
      getGamelist,
      searchGame,
      getApprovedGameList
    }
  },
  {
    persist: true
  }
)
